/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import util.IncompleteFile;
import util.RemoteFile;
import views.DownloadItem;
import views.MainWindow;

/**
 *
 * @author Victor
 */
public class MainWindowController {
    
    private final DatabaseController dbController;
    private final MainWindow mainWindow;
    private final DownloadsController downloadsController;
    private int downloadId = 0;
    
    
    public MainWindowController(MainWindow mainWindow, DatabaseController dbController){
        this.dbController = dbController;
        this.mainWindow = mainWindow;
        this.downloadsController = new DownloadsController(dbController);
    }

    void addDownload(RemoteFile downloadFile) {
        try {
            IncompleteFile file = downloadsController.createDownload(downloadFile);
            new Thread(file,"Download Manager: " + downloadFile.getName()).start(); //Start the download manager thread
            mainWindow.addDownloadItem(new DownloadItem(generateDownloadId(), file));
        } catch (IOException ex) {
            ex.printStackTrace();
            System.err.println("Erro ao criar download.");
        }
    }
    
    public DatabaseController getDBController() {
        return dbController;
    }
    
    //PRECISA SER TROCADO PARA SUPORTAR REINICIALIZAÇÕES DO PROGRAMA
    private int generateDownloadId(){
        downloadId++;
        return downloadId;
    }

    public void showWindow() {
       this.mainWindow.setVisible(true);
    }
    
}
